package com.sanmubird.mywebframework.indicator.domain.executor;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Arrays;
import java.util.stream.Collectors;


/**
 * @author sam
 */
public class SqlFunctions {

    private SqlFunctions() {
    }


    public static String forEach(Object[] list) {
        return Arrays.stream(list).map(s -> {
            if (s instanceof Number) {
                return s.toString();
            }
            return "'" + s + "'";
        }).collect(Collectors.joining(","));
    }

    public static YearMonth nowYearMonth() {
        return YearMonth.now();
    }

    public static String formatDate(TemporalAccessor temporalAccessor, String format) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern(format);
        return pattern.format(temporalAccessor);
    }

    public static LocalDateTime nowTime() {
        return LocalDateTime.now();
    }

    public static YearMonth parseToYearMonth(String yearMonthString) {
        return YearMonth.parse(yearMonthString);
    }


    public static LocalDateTime parseToLocalDateTimeFromMillis(long millis) {
        return Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }


}
